package com.aipanda.entity;

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;

/**
 * 优惠券码类
 * @param 	code		优惠券码
 * @param 	isUsed		是否使用
 * @param 	usedDate	使用时间
 * @param 	coupon      优惠券类型[多对一关系]
 * @param 	member		会员[多对一关系]
 * @param 	order		订单[一对一关系]
 * @author wanyuxiang
 *
 */
@Entity
@Table(name = "aipanda_coupon_code")
public class CouponCode extends BaseEntity {
	private static final long serialVersionUID = -1812874037224306719L;
	private String code;
	private Boolean isUsed;
	private Date usedDate;
	private Coupon coupon;
	private Member member;
	private Order order;

	@Column(nullable = false, updatable = false, unique = true)
	public String getCode() {
		return this.code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	@Column(name="is_used",nullable = false)
	public Boolean getIsUsed() {
		return this.isUsed;
	}

	public void setIsUsed(Boolean isUsed) {
		this.isUsed = isUsed;
	}

	@Column(name="used_date")
	public Date getUsedDate() {
		return this.usedDate;
	}

	public void setUsedDate(Date usedDate) {
		this.usedDate = usedDate;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="coupon",nullable = false, updatable = false)
	public Coupon getCoupon() {
		return this.coupon;
	}

	public void setCoupon(Coupon coupon) {
		this.coupon = coupon;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="member")
	public Member getMember() {
		return this.member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	@OneToOne(mappedBy = "couponCode", fetch = FetchType.LAZY)
	public Order getOrder() {
		return this.order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	public CouponCode preRemove(CouponCode couponCode) {
		if (couponCode.getOrder() != null){
			couponCode.getOrder().setCouponCode(null);
		}
		return couponCode;
	}
}
